package com.mhc.haval.service.impl;

import com.mhc.haval.api.dto.DepartmentDTO;
import com.mhc.haval.api.dto.DeptBizAreaDTO;
import com.mhc.haval.api.dto.StaffRoleDTO;
import com.mhc.haval.api.service.IStaffDeptService;
import com.mhc.haval.bo.DepartmentInfoBO;
import com.mhc.haval.constant.IsDeletedEnum;
import com.mhc.haval.dal.mapper.ext.DepartmentExtMapper;
import com.mhc.haval.dal.model.DepartmentDO;
import com.mhc.haval.dal.model.DeptBizAreaDO;
import com.mhc.haval.dal.model.StaffDO;
import com.mhc.haval.dal.query.DeptBizAreaQuery;
import com.mhc.haval.manager.DepartmentManager;
import com.mhc.haval.manager.DeptBizAreaManager;
import com.subaru.common.util.BeanCopierUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by HMJ on 2017/9/25.
 */
@Slf4j
@RestController
public class StaffDeptImpl implements IStaffDeptService{

    @Autowired
    private DepartmentManager departmentManager;

    @Autowired
    private DepartmentExtMapper departmentExtMapper;

    @Autowired
    private DeptBizAreaManager deptBizAreaManager;

    /**
     * 查询部门列表
     * @param parentDeptId    上级部门ID
     * @param companyNo       公司ID
     * @return
     * @throws Exception
     */
    @Override
    public List<DepartmentDTO> queryByDeptList(Long parentDeptId, String companyNo){
        log.info("开始查询部门列表...");
        Map<String, Object> paramMap = new HashMap<>(64);
        paramMap.put("parentDeptId",parentDeptId);
        paramMap.put("companyNo",companyNo);
        /**根据公司ID和上级部门ID查询部门列表**/
        List<DepartmentInfoBO> deptLists = departmentExtMapper.queryByDeptList(paramMap);
        List<DepartmentDTO> departmentDTOList = new ArrayList<>();
        for(DepartmentInfoBO departmentInfoBO : deptLists){
            DepartmentDTO departmentDTO = new DepartmentDTO();
            BeanCopierUtil.copy(departmentInfoBO,departmentDTO);
            departmentDTOList.add(departmentDTO);
        }
        return  departmentDTOList;
    }

    /**
     * 查看部门详情信息
     * @param deptId    部门ID
     * @return
     */
    @Override
    public DepartmentDTO queryByInfo(Long deptId){
        DepartmentDO deptDO = new DepartmentDO();
        DepartmentDTO departmentDTO = new DepartmentDTO();
        if(deptId!=null){
            deptDO = departmentManager.selectByPrimaryKey(deptId);
        }
        BeanUtils.copyProperties(deptDO, departmentDTO);
        return departmentDTO;
    }

    /**
     * 根据部门和角色号查询所属员工
     * @param deptId
     * @param roleCode
     * @return
     */
    @Override
    public List<StaffRoleDTO> selectStaffByDept(Long deptId, String roleCode){
        List<StaffRoleDTO> staffDTOList = new ArrayList<>();
        if(deptId!=null){
            Map<String, Object> paramMap = new HashMap<>(64);
            paramMap.put("deptId",deptId);
            paramMap.put("roleCode",roleCode);
            List<StaffDO> staffDOList = departmentExtMapper.queryStaffByDept(paramMap);
            for(StaffDO staffDO : staffDOList){
                StaffRoleDTO staffRoleDTO = new StaffRoleDTO();
                BeanUtils.copyProperties(staffDO, staffRoleDTO);
                staffDTOList.add(staffRoleDTO);
            }
        }
        return staffDTOList;
    }

    /**
     * 根据省份ID查询所属部门列表
     * @param areaId
     * @return
     */
    @Override
    public List<DeptBizAreaDTO> selectBizAreaDeptList(Long areaId){
        log.info("正在查询符合条件的部门...");
        DeptBizAreaQuery deptBizAreaQuery = new DeptBizAreaQuery();
        deptBizAreaQuery.createCriteria().andAreaIdEqualTo(areaId).andIsDeletedEqualTo(IsDeletedEnum.NORMAL.getCode());
        List<DeptBizAreaDO> deptBizAreaDOList = deptBizAreaManager.selectByQuery(deptBizAreaQuery);
        List<DeptBizAreaDTO> deptBizAreaDTOList = new ArrayList<>();
        for(DeptBizAreaDO deptBizAreaDO : deptBizAreaDOList ){
            DeptBizAreaDTO deptBizAreaDTO = new DeptBizAreaDTO();
            BeanUtils.copyProperties(deptBizAreaDO, deptBizAreaDTO);
            deptBizAreaDTOList.add(deptBizAreaDTO);
        }
        return deptBizAreaDTOList;
    }

}
